﻿layui.use(['form', 'layer', 'table', 'laytpl', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;


    function loadDate() {
        laydate.render({
            elem: '#startdate'
        });
        laydate.render({
            elem: '#enddate'
        });
    }
    
    var tableIns = table.render({
        elem: '#managerList',
        url: '/InOutCash/LoadOutCashRecordData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 40 },
            { field: "Id", title: '序号', width: 70, align: "center" },
            { field: 'UserRealName', title: '姓名', width: 100, align: "center", totalRowText: '合计:' },
            { field: 'OrderNo', title: '订单号', minWidth: 50, align: "center" },
            { field: 'Money', title: '金额(美元)', width: 110, align: "center", totalRow: true },
            { field: 'ExchangeRate', title: '汇率', width: 70, align: "center" },
            { field: 'RMBMoney', title: '金额(RMB)', minWidth: 80, align: "center", totalRow: true },
            { field: 'AddTime', title: '申请时间', minWidth: 80, align: "center" },
            // { field: 'AuditTime', title: '审核时间', minWidth: 80, align: "center" },
            { field: 'UserCardNo', title: '卡号', minWidth: 80, align: "center" },
            { field: 'Remark', title: '备注', minWidth: 80, align: "center" },
            { title: '操作', minWidth: 250, templet: '#managerListBar', fixed: "right", align: "center" }
        ]],
        totalRow: true  //开启合计行
    });

    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'audit') { //审核并打款
            var addTime = (data.AddTime || '').split(' ')[1].split(':')[0];
            var outMoney = 5;
            if (addTime >= 9 && addTime < 18) {
                outMoney = 3;
            }
            var __outMoney = data.RMBMoney - outMoney;
            layer.confirm('用户出金【￥' + __outMoney + '】元<br /> 9:00~18:00将扣除3元，18:00以后将扣除5元<br />手续费，是否继续？', { icon: 3, title: '出金审核' }, function (index) {
                //获取防伪标记
                audit(data);
            });
        }else if (layEvent === 'auditGomePay') { // 美付宝代付审核并打款
            var addTime = (data.AddTime || '').split(' ')[1].split(':')[0];
            var outMoney = 5;
            if (addTime >= 9 && addTime < 18) {
                outMoney = 3;
            }
            var __outMoney = data.RMBMoney - outMoney;
            var _customer = "<br/><h3 style=color:red>请确认以下信息</h3>发卡行：" + data.UserBankType + "<br />账户名：" + data.UserRealName + "<br />银行卡号：" + data.UserCardNo;
            //layer.confirm('用户出金【￥' + __outMoney + '】元<br /> 9:00~18:00将扣除3元<br />18:00以后将扣除5元 手续费'+_customer+'<br/>是否继续？', { icon: 3, title: '出金审核' }, function (index) {
            //    //获取防伪标记
            //    auditGomePay(data);
            //});
            var confirmHtml = '用户出金【￥' + __outMoney + '】元<br /> 9:00~18:00将扣除3元<br />18:00以后将扣除5元 手续费' + _customer + '<br/><div  style=padding-top:30px><input id="txt_gomePayPwd" name="paypwd" style="height: 38px;line-height: 1.3;background-color: #fff;outline: 0;" type="password" autocomplete="off" placeholder="请输入支付密码" value=""/></div>';
            var htmlStr = '<div style = "padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;" >' + confirmHtml+'</div> ';

            layer.open({
                type: 1
                , title: false //不显示标题栏
                , closeBtn: false
                , area: '500px;'
                , shade: 0.2
                , id: 'btn_auditGomePay' //设定一个id，防止重复弹出
                , btn: ['付款', '取消']
                , btnAlign: 'c'
                , moveType: 1 //拖拽模式，0或者1
                , content:htmlStr
                , yes: function (index, layero) {
                    var pwdValue = layero[0].querySelectorAll("#txt_gomePayPwd")[0].value;
                    var str = window.btoa(pwdValue);
                    auditGomePay(data, str, index);
                }
                ,btn2:function (index, layero) {
                   
                }
            });
        } else if (layEvent === 'balance') { //查询余额
            balance(data);
        } else if (layEvent === 'cancel'){ //取消此订单
            layer.confirm('您确认要拒绝此出金记录吗？', { icon: 3, title: '出金审核' }, function (index) {
                //获取防伪标记
                cancel(data);
            });
        }
    });

    function audit(item) {
        //console.log(item);
        $.ajax({
            type: 'POST',
            url: '/InOutCash/AuditOutCoin/?inCoinId=' + item.Id,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }
    ///美付宝代付
    function auditGomePay(item, pwd, parentIndex)
    {
      var loadingIndex = layer.load(3, {
            shade:0.8 
        });
        $.ajax({
            type: 'POST',
            url: '/InOutCash/AuditGomePayOutCoin/?inCoinId=' + item.Id + '&gomePwd='+pwd,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数

                var index = layer.alert(data.ResultMsg, { closeBtn: 0},function () {
                    tableIns.reload();
                    if (data.ResultCode == 200) {
                        layer.closeAll();
                    } else {
                        layer.close(index);
                        layer.close(loadingIndex);
                    }
                   
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }
    function balance(item) {
        var loadingIndex = layer.load(3, {
            shade: 0.8
        });
        $.ajax({
            type: 'POST',
            url: '/InOutCash/GetAccountBalance/?username=' + item.UserName,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                if (data.Success == true) {
                    var showHtml = " <div> 可用余额:" + data.Data.BuyingPower + "</div><div>货币单位:" + data.Data.Currency + "</div>";
                    layer.alert(showHtml);
                }
                layer.close(loadingIndex);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                layer.close(loadingIndex);
            }

        });
    }

    function cancel(item) {
        $.ajax({
            type: 'POST',
            url: '/InOutCash/CancelOutCoin/?inCoinId=' + item.Id,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        //if ($(".searchVal").val() !== '') {
        table.reload("managerListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                key: $(".searchVal").val(),  //搜索的关键字
                state: $(".searchState").val(),
                start: $(".startdate").val(),
                end: $(".enddate").val(),
                managerId: $(".searchManagerId").val()
            }
        });
        //} else {
        //    layer.msg("请输入搜索的内容");
        //}
    });

    loadDate();

});